package com.zhao.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhao.comm.log.MyLog;
import com.zhao.pojo.*;
import com.zhao.service.*;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.util.List;

@Controller
@RequestMapping("/group")
public class GroupController {

    @Autowired
    GroupsService groupsService;
    @Autowired
    ArticleService articleService;
    @Autowired
    StatisticService statisticService;
    @Autowired
    CommentService commentService;
    //资源分组部分
    @Autowired
    FilegroupService filegroupService;
    @Autowired
    FileStatisticService fileStatisticService;
    @Autowired
    FileCommentService fileCommentService;
    @Autowired
    DatafileService datafileService;
    @Autowired
    RoleService roleService;

    @RequestMapping("/data_group")
    public String dataGroup(Model model,
                            @RequestParam(value = "title",required = false)String title,
                            @RequestParam(value = "cut",required = false,defaultValue = "1")Integer cut) {
        User loginUser = (User) SecurityUtils.getSubject().getPrincipal();
        String roleName=roleService.getUserRoleName(loginUser.getUserId());
       if (cut==1) {
            QueryWrapper<Groups> gname = new QueryWrapper<Groups>();
            if (title!=null) {
                gname.like("gname", title);
                model.addAttribute("title",title);
            }
           if (roleName.equals("用户")){
               gname.eq("uid",loginUser.getUserId());
           }
            List<Groups> groups = groupsService.list(gname);
            model.addAttribute("groups", groups);
            model.addAttribute("cut",1);
       }else {
           QueryWrapper<Filegroup> gname = new QueryWrapper<Filegroup>();
           if (title!=null) {
               gname.like("fgname", title);
               model.addAttribute("title",title);
           }
           if (roleName.equals("用户")){
               gname.eq("uid",loginUser.getUserId());
           }
           List<Filegroup> filegroups = filegroupService.list(gname);
           model.addAttribute("filegroups",filegroups);
           model.addAttribute("cut",0);
       }
        return "admin/data_group";
    }
    //修改 分组名 或者 前台显示状态
    //@MyLog(value = "修改文章分组名/是否对外显示")
    @ResponseBody
    @RequestMapping("/updateGroup/{id}/{code}/{num}")
    public String updateGroup(@PathVariable String id, @PathVariable String code, @PathVariable String num) {
        //code等于1 修改标题 否则修改显示
        if (code.equals("1")) {
            Groups one = groupsService.getById(id);
            one.setGname(num);
            groupsService.updateById(one);
        }
        if (code.equals("0")) {
            Groups one = groupsService.getOne(new QueryWrapper<Groups>().eq("groups_id", id));
            if (num.equals("0")) {
                one.setGshow(1);
            } else {
                one.setGshow(0);
            }
            groupsService.updateById(one);
        }
        return "1";
    }

    //@MyLog(value = "删除文章分组")
    @RequestMapping("/delGroup/{id}")
    public String delGroup(@PathVariable Integer id) {
        List<Article> articles = articleService.list(new QueryWrapper<Article>().eq("groups_id", id));
        if (articles != null) {
            for (Article article : articles) {
                commentService.remove(new QueryWrapper<Comment>().eq("article_id", article.getArticleId()));
                statisticService.remove(new QueryWrapper<Statistic>().eq("article_id", article.getArticleId()));
                articleService.removeById(article.getArticleId());
            }
        }
        groupsService.removeById(id);
        return "redirect:/group/data_group";
    }
    @RequestMapping("/addGroup/{title}")
    public String addGroup(@PathVariable String title) {
        Groups groups=new Groups();
        User loginUser = (User) SecurityUtils.getSubject().getPrincipal();
        groups.setGname(title);
        groups.setUid(loginUser.getUserId());
        groupsService.save(groups);
        return "redirect:/group/data_group";
    }
    // 资源分栏部分top
    //修改 分组名 或者 前台显示状态
   // @MyLog(value = "修改资源分组名/是否对外显示")
    @ResponseBody
    @RequestMapping("/updateFileGroup/{id}/{code}/{num}")
    public String updateFileGroup(@PathVariable String id, @PathVariable String code, @PathVariable String num) {
        //code等于1 修改标题 否则修改显示
        System.out.println(id+"-------"+code+"-------"+num);
        if (code.equals("1")) {
            Filegroup byId = filegroupService.getById(id);
            byId.setFgname(num);
            filegroupService.updateById(byId);
        }
        if (code.equals("0")) {
            Filegroup byId = filegroupService.getById(id);
            if (num.equals("0")) {
                byId.setGshow(1);
            } else {
                byId.setGshow(0);
            }
            filegroupService.updateById(byId);
        }
        return "1";
    }

    //@MyLog(value = "创建新资源组")
    @RequestMapping("/addFileGroup/{title}")
    public String addFileGroup(@PathVariable String title) {
        Filegroup filegroup=new Filegroup();
        User loginUser = (User) SecurityUtils.getSubject().getPrincipal();
        filegroup.setUid(loginUser.getUserId());
        filegroup.setFgname(title);
        filegroupService.save(filegroup);
        return "redirect:/group/data_group?cut=0";
    }
   // @MyLog(value = "删除资源组")
    @RequestMapping("/delFileGroup/{id}")
    public String delFileGroup(@PathVariable Integer id) {
        List<Datafile> datafiles = datafileService.list(new QueryWrapper<Datafile>().eq("filegroup_id", id));
        if (datafiles!=null){
            for (Datafile datafile:datafiles) {
                fileCommentService.remove(new QueryWrapper<FileComment>().eq("file_id",datafile.getFileId()));
                QueryWrapper<Datafile> wrapper=new QueryWrapper<>();
                wrapper.eq("file_id",datafile.getFileId());
                Datafile one = datafileService.getOne(wrapper);
                //删除本地文件
                System.out.println(one.getNewname());
                boolean flag=false;
                if (one!=null){
                    File file = new File(one.getAbsolutepath());
                    if (file.exists()) {
                        file.delete();
                        System.out.println("文件已经被成功删除");
                        flag=true;
                    }
                }
                System.out.println(flag);
                if (flag){
                    System.out.println(datafile.getFileId());
                    //删除 资源信息统计表中对应数据
                    fileStatisticService.remove(new QueryWrapper<Filestatistic>().eq("file_id",datafile.getFileId()));
                    //删除 数据
                    datafileService.removeById(datafile.getFileId());
                }
            }
        }
        filegroupService.removeById(id);
        return "redirect:/group/data_group?cut=0";
    }
}
